<template>
  <div class="settings-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first">
            <el-button type="primary" icon="el-icon-plus" @click="dialogFormVisible = true">新增角色</el-button>

            <!-- 表格 -->
            <el-table :data="roleList" style="width: 100%">
              <el-table-column label="序号" type="index" />
              <el-table-column label="角色名称" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="mini" type="success" @click="handleEdit(scope.$index, scope.row)">分配权限</el-button>
                  <el-button
                    size="mini"
                    type="primary"
                    @click="handleEdit(scope.$index, scope.row)"
                  >编辑</el-button>
                  <el-button
                    size="mini"
                    type="danger"
                    @click="handleDelete(scope.$index, scope.row)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <!-- 分页 -->
            <div class="pagination">
              <el-pagination
                :current-page="page"
                :page-sizes="[4, 6, 8, 10]"
                :page-size="pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </div>
          </el-tab-pane>
        </el-tabs>

        <!-- 弹窗 -->
        <el-dialog
          title="新增角色"
          :visible.sync="dialogFormVisible"
          :close-on-click-modal="false"
          :close-on-press-escape="true"
        >
          <el-form :model="form" label-width="120px">
            <el-form-item label="角色名称" prop="name">
              <el-input v-model="form.name" style="width: 80%;" />
            </el-form-item>
            <el-form-item label="角色描述" prop="description">
              <el-input v-model="form.description" style="width: 80%;" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="doAddRole">确 定</el-button>
              <el-button @click="dialogFormVisible = false">取 消</el-button>
            </el-form-item>
          </el-form>
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { deleteRole, getRoleList, addRole } from '@/api/settings'
export default {
  data() {
    return {
      activeName: 'first',
      roleList: [], // 角色列表
      page: 1, // 当前页
      pagesize: 4, // 每页条数
      total: 0, // 总条数
      dialogFormVisible: false,
      form: {
        name: '',
        description: ''
      }
    }
  },
  created() {
    this.loadRoleList()
  },
  methods: {
    // 渲染角色列表
    async loadRoleList() {
      const res = await getRoleList({ page: this.page, pagesize: this.pagesize })
      console.log('角色列表', res)
      this.roleList = res.data.rows
      this.total = res.data.total
    },
    handleEdit(index, row) {
      console.log(index, row)
    },
    // 删除角色
    handleDelete(index, row) {
      console.log(index, row)

      this.$confirm('确定删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteRole(row.id)
        this.$message.success('删除成功')
        this.loadRoleList()
      })
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      // 从第一页开始发请求
      this.page = 1
      this.pagesize = val
      this.loadRoleList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.page = val
      this.loadRoleList()
    },
    // 点击确认按钮
    async doAddRole() {
      // console.log(this.form)
      console.log(this.form)
      const res = await addRole(this.form)
      console.log(res)
      this.dialogFormVisible = false
      this.loadRoleList()
    }
  }
}
</script>

<style lang="scss" scoped>
.pagination{
  text-align: center;
  margin-top: 20px;
}
</style>
